<!--
 * @Author: 赵克强 huohuo625@163.com
 * @Date: 2023-11-06 15:19:02
 * @LastEditors: 赵克强 huohuo625@163.com
 * @LastEditTime: 2024-05-07 15:01:43
 * @FilePath: \hy-ui\src\views\render\form\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="form" style="margin: 12px" v-loading="loading">
    <el-card class="box-card" style="padding: 12px">
      <div slot="header" class="clearfix">
        <span>{{ formConfig.formName }}</span>
      </div>
      <div class="main">
        <div>
          <v-form-render ref="vFormRef" :option-data="formConfig.dictList">
          </v-form-render>
        </div>
        <div class="btn">
          <div>
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  getFormConfig,
  appDictData,
  saveOrUpdate,
} from "@/api/render/pager.js";
import request from "@/utils/request";
export default {
  name: "form",

  data() {
    return {
      formConfig: {},
      loading: false,
    };
  },

  async mounted() {
    if (
      this.$route.path.includes(this.firstLetterToLowerCase(this.$route.name))
    ) {
      this.loading = true;
      const res = await getFormConfig(
        this.firstLetterToLowerCase(this.$route.name)
      );
      if (res.code === 200 && res.data.formConfig && res.data.widgetList) {
        let modalConfig = {};
        modalConfig = {
          ...res.data,
          formConfig: JSON.parse(res.data.formConfig),
          widgetList: JSON.parse(res.data.widgetList),
        };
        this.formConfig = modalConfig;
        let formJson = {
          formConfig: this.formConfig?.formConfig,
          widgetList: this.formConfig?.widgetList,
        };
        this.$refs.vFormRef.setFormJson(formJson);
        this.loading = false;
      }
    } else if (this.$route.path.includes(this.$route.name)) {
      this.loading = true;
      const res = await getFormConfig(
        this.firstLetterToLowerCase(this.$route.name)
      );
      if (res.code === 200 && res.data.formConfig && res.data.widgetList) {
        let modalConfig = {};
        modalConfig = {
          ...res.data,
          formConfig: JSON.parse(res.data.formConfig),
          widgetList: JSON.parse(res.data.widgetList),
        };
        this.formConfig = modalConfig;
        let formJson = {
          formConfig: this.formConfig?.formConfig,
          widgetList: this.formConfig?.widgetList,
        };
        this.$refs.vFormRef.setFormJson(formJson);
        this.loading = false;
      }
    } else {
      return this.$message.error("未知错误,请联系管理员");
    }
  },

  methods: {
    firstLetterToLowerCase(str) {
      return str.charAt(0).toLowerCase() + str.slice(1);
    },
    handleReset() {
      this.$refs.vFormRef.resetForm();
    },
    handleSubmit() {
      // const oldValue = this.formConfig?.detailData
      if (this.formConfig.formConfig?.onFormSaveBefore) {
        new Function(this.formConfig.formConfig?.onFormSaveBefore)();
      }
      this.$refs.vFormRef.validateForm(async (valid) => {
        if (valid) {
          const params = await this.$refs.vFormRef.getFormData();
          if (this.formConfig.formConfig.erMark) {
            params.erMark = this.formConfig.formConfig.erMark;
          }
          const submit = async () => {
            const res = await saveOrUpdate(this.formConfig?.tableName, params);
            if (res.code === 200) {
              if (this.formConfig.formConfig.afterAddForJava) {
                await request({
                  url: this.formConfig.formConfig.afterAddForJava,
                  method: "post",
                  data: res?.data,
                });
              }
              // if (this.formConfig.formConfig.afterUpdateForJava) {
              //   await request({
              //     url: this.formConfig.formConfig.afterUpdateForJava,
              //     method: "post",
              //     data: {
              //       oldValue,
              //       newValue: params,
              //     },
              //   });
              // }
              if (this.formConfig.formConfig?.onFormSaveAfter) {
                new Function(this.formConfig.formConfig?.onFormSaveAfter)();
              }
              this.$message.success(res.msg);
              this.$refs.vFormRef.resetForm();
            }
          };
          if (this.formConfig.formConfig.afterAddForJava) {
            await request({
              url: this.formConfig.formConfig.afterAddForJava,
              method: "post",
              data: params,
            });
            if (res.code === 200) {
              await submit();
            } else {
              this.$message.error(res.message);
            }
          }else{
            submit()
          }
          // else if (this.formConfig.formConfig.beforeUpdateForJava) {
          //   const res = await request({
          //     url: this.formConfig.formConfig.beforeUpdateForJava,
          //     method: "post",
          //     data:{
          //           oldValue,
          //           newValue:params
          //         },
          //   });
          //   if (res.code === 200) {
          //     await submit();
          //   } else {
          //     this.$message.error(res.message);
          //   }
          // }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  ::v-deep .el-card {
    // min-height: 88vh !important;
  }
  ::v-deep .el-form-item__error {
    z-index: 95;
  }
  .main {
    min-height: 73vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>